home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / Berkeley DB 1.6 / recno / rec_get.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-26  |  6.4 KB  |  288 lines  |  [TEXT/????]

  1. /*-
  2.  * Copyright (c) 1990, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)rec_get.c    8.1 (Berkeley) 6/4/93";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #include <sys/types.h>
  39.  
  40. #include <errno.h>
  41. #include <stddef.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #ifndef macintosh
  46. #include <unistd.h>
  47. #endif
  48.  
  49. #include <db.h>
  50. #include "recno.h"
  51.  
  52. /*
  53.  * __REC_GET -- Get a record from the btree.
  54.  *
  55.  * Parameters:
  56.  *    dbp:    pointer to access method
  57.  *    key:    key to find
  58.  *    data:    data to return
  59.  *    flag:    currently unused
  60.  *
  61.  * Returns:
  62.  *    RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
  63.  */
  64. int
  65. __rec_get(dbp, key, data, flags)
  66.     const DB *dbp;
  67.     const DBT *key;
  68.     DBT *data;
  69.     u_int flags;
  70. {
  71.     BTREE *t;
  72.     EPG *e;
  73.     recno_t nrec;
  74.     int status;
  75.  
  76.     if (flags || (nrec = *(recno_t *)key->data) == 0) {
  77.         errno = EINVAL;
  78.         return (RET_ERROR);
  79.     }
  80.  
  81.     /*
  82.      * If we haven't seen this record yet, try to find it in the
  83.      * original file.
  84.      */
  85.     t = dbp->internal;
  86.     if (nrec > t->bt_nrecs) {
  87.         if (ISSET(t, R_EOF | R_INMEM))
  88.             return (RET_SPECIAL);
  89.         if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS)
  90.             return (status);
  91.     }
  92.  
  93.     --nrec;
  94.     if ((e = __rec_search(t, nrec, SEARCH)) == NULL)
  95.         return (RET_ERROR);
  96.  
  97.     status = __rec_ret(t, e, 0, NULL, data);
  98.     mpool_put(t->bt_mp, e->page, 0);
  99.     return (status);
  100. }
  101.  
  102. /*
  103.  * __REC_FPIPE -- Get fixed length records from a pipe.
  104.  *
  105.  * Parameters:
  106.  *    t:    tree
  107.  *    cnt:    records to read
  108.  *
  109.  * Returns:
  110.  *    RET_ERROR, RET_SUCCESS
  111.  */
  112. int
  113. __rec_fpipe(t, top)
  114.     BTREE *t;
  115.     recno_t top;
  116. {
  117.     DBT data;
  118.     recno_t nrec;
  119.     size_t len;
  120.     int ch;
  121.     char *p;
  122.  
  123.     data.data = t->bt_dbuf;
  124.     data.size = t->bt_reclen;
  125.  
  126.     if (t->bt_dbufsz < t->bt_reclen) {
  127.         if ((t->bt_dbuf = realloc(t->bt_dbuf, t->bt_reclen)) == NULL)
  128.             return (RET_ERROR);
  129.         t->bt_dbufsz = t->bt_reclen;
  130.     }
  131.     for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
  132.         len = t->bt_reclen;
  133.         for (p = t->bt_dbuf;; *p++ = ch)
  134.             if ((ch = getc(t->bt_rfp)) == EOF || !len--) {
  135.                 if (__rec_iput(t, nrec, &data, 0)
  136.                     != RET_SUCCESS)
  137.                     return (RET_ERROR);
  138.                 break;
  139.             }
  140.         if (ch == EOF)
  141.             break;
  142.     }
  143.     if (nrec < top) {
  144.         SET(t, R_EOF);
  145.         return (RET_SPECIAL);
  146.     }
  147.     return (RET_SUCCESS);
  148. }
  149.  
  150. /*
  151.  * __REC_VPIPE -- Get variable length records from a pipe.
  152.  *
  153.  * Parameters:
  154.  *    t:    tree
  155.  *    cnt:    records to read
  156.  *
  157.  * Returns:
  158.  *    RET_ERROR, RET_SUCCESS
  159.  */
  160. int
  161. __rec_vpipe(t, top)
  162.     BTREE *t;
  163.     recno_t top;
  164. {
  165.     DBT data;
  166.     recno_t nrec;
  167.     indx_t len;
  168.     size_t sz;
  169.     int bval, ch;
  170.     char *p;
  171.  
  172.     bval = t->bt_bval;
  173.     for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
  174.         for (p = t->bt_dbuf, sz = t->bt_dbufsz;; *p++ = ch, --sz) {
  175.             if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) {
  176.                 data.data = t->bt_dbuf;
  177.                 data.size = p - t->bt_dbuf;
  178.                 if (ch == EOF && data.size == 0)
  179.                     break;
  180.                 if (__rec_iput(t, nrec, &data, 0)
  181.                     != RET_SUCCESS)
  182.                     return (RET_ERROR);
  183.                 break;
  184.             }
  185.             if (sz == 0) {
  186.                 len = p - t->bt_dbuf;
  187.                 t->bt_dbufsz += (sz = 256);
  188.                 if ((t->bt_dbuf =
  189.                     realloc(t->bt_dbuf, t->bt_dbufsz)) == NULL)
  190.                     return (RET_ERROR);
  191.                 p = t->bt_dbuf + len;
  192.             }
  193.         }
  194.         if (ch == EOF)
  195.             break;
  196.     }
  197.     if (nrec < top) {
  198.         SET(t, R_EOF);
  199.         return (RET_SPECIAL);
  200.     }
  201.     return (RET_SUCCESS);
  202. }
  203.  
  204. /*
  205.  * __REC_FMAP -- Get fixed length records from a file.
  206.  *
  207.  * Parameters:
  208.  *    t:    tree
  209.  *    cnt:    records to read
  210.  *
  211.  * Returns:
  212.  *    RET_ERROR, RET_SUCCESS
  213.  */
  214. int
  215. __rec_fmap(t, top)
  216.     BTREE *t;
  217.     recno_t top;
  218. {
  219.     DBT data;
  220.     recno_t nrec;
  221.     caddr_t sp, ep;
  222.     size_t len;
  223.     char *p;
  224.  
  225.     sp = t->bt_cmap;
  226.     ep = t->bt_emap;
  227.     data.data = t->bt_dbuf;
  228.     data.size = t->bt_reclen;
  229.  
  230.     if (t->bt_dbufsz < t->bt_reclen) {
  231.         if ((t->bt_dbuf = realloc(t->bt_dbuf, t->bt_reclen)) == NULL)
  232.             return (RET_ERROR);
  233.         t->bt_dbufsz = t->bt_reclen;
  234.     }
  235.     for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
  236.         if (sp >= ep) {
  237.             SET(t, R_EOF);
  238.             return (RET_SPECIAL);
  239.         }
  240.         len = t->bt_reclen;
  241.         for (p = t->bt_dbuf; sp < ep && len--; *p++ = *sp++);
  242.         memset(p, t->bt_bval, len);
  243.         if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
  244.             return (RET_ERROR);
  245.     }
  246.     t->bt_cmap = sp;
  247.     return (RET_SUCCESS);
  248. }
  249.  
  250. /*
  251.  * __REC_VMAP -- Get variable length records from a file.
  252.  *
  253.  * Parameters:
  254.  *    t:    tree
  255.  *    cnt:    records to read
  256.  *
  257.  * Returns:
  258.  *    RET_ERROR, RET_SUCCESS
  259.  */
  260. int
  261. __rec_vmap(t, top)
  262.     BTREE *t;
  263.     recno_t top;
  264. {
  265.     DBT data;
  266.     caddr_t sp, ep;
  267.     recno_t nrec;
  268.     int bval;
  269.  
  270.     sp = t->bt_cmap;
  271.     ep = t->bt_emap;
  272.     bval = t->bt_bval;
  273.  
  274.     for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
  275.         if (sp >= ep) {
  276.             SET(t, R_EOF);
  277.             return (RET_SPECIAL);
  278.         }
  279.         for (data.data = sp; sp < ep && *sp != bval; ++sp);
  280.         data.size = sp - (caddr_t)data.data;
  281.         if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
  282.             return (RET_ERROR);
  283.         ++sp;
  284.     }
  285.     t->bt_cmap = sp;
  286.     return (RET_SUCCESS);
  287. }
  288.